/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Anonymous
 */
public class MaHoa {
    //Hàm mã hóa dữ liệu string -> string
    public static String MaHoa(String raw)
    {
        String rs = null;
        FileInputStream is = null;
       try {
           is = new FileInputStream("key.txt"); //duong dan toi khoa
       } catch (FileNotFoundException ex) {
           Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
               byte[] buffer = null;
       try {
           buffer = new byte[is.available()-2];
       } catch (IOException ex) {
           Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
               while(true) 
               {
                    int read = 0;
            try {
                read = is.read(buffer);
            } catch (IOException ex) {
                Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
                    if ( read < 0 ) 
                    {
                       break;
                    }

        }                    
        System.out.println("byte Encryted : " + buffer);
        SecretKeySpec myDesKey = new SecretKeySpec(buffer,"DES");
       Cipher desCipher = null;
       try {
           desCipher = Cipher.getInstance("DES");
       } catch (    NoSuchAlgorithmException | NoSuchPaddingException ex) {
           Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
       try {
           desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
       } catch (InvalidKeyException ex) {
           Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
       //ma hoa ga bat dau
       byte[] gabatdau = raw.getBytes();
       byte[] textEncryptedgabd = null;   
       try {
           textEncryptedgabd = desCipher.doFinal(gabatdau);
       } catch (    IllegalBlockSizeException | BadPaddingException ex) {
           Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
       BASE64Encoder encoder1=new BASE64Encoder();
       rs=encoder1.encode(textEncryptedgabd);
        return rs;
    }
    
    //Hàm giải mã dữ liệu string -> string
    public static String GiaiMa(String data)
    {
        String rs = null;
        FileInputStream is = null;
       try {
           is = new FileInputStream("key.txt"); //duong dan toi khoa
       } catch (FileNotFoundException ex) {
           Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
               byte[] buffer = null;
       try {
           buffer = new byte[is.available()-2];
       } catch (IOException ex) {
           Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
               while(true) 
               {
                    int read = 0;
            try {
                read = is.read(buffer);
            } catch (IOException ex) {
                Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
                    if ( read < 0 ) 
                    {
                       break;
                    }

        }                    
        System.out.println("byte Encryted : " + buffer);
        SecretKeySpec myDesKey = new SecretKeySpec(buffer,"DES");
       Cipher desCipher = null;
            try {
                desCipher = Cipher.getInstance("DES");
            } catch (    NoSuchAlgorithmException | NoSuchPaddingException ex) {
                Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                desCipher.init(Cipher.DECRYPT_MODE,myDesKey );
            } catch (InvalidKeyException ex) {
                Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            BASE64Decoder decodergabd=new BASE64Decoder();
            byte[] tam = null;             
            try {
                tam = decodergabd.decodeBuffer(data);
            } catch (IOException ex) {
                Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            byte[] textDecryptedgabd = null;
            try {
                textDecryptedgabd = desCipher.doFinal(tam);
            } catch (    IllegalBlockSizeException | BadPaddingException ex) {
                Logger.getLogger(ChuyenXeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            rs=new String(textDecryptedgabd);
        return rs;
    }
}
